from ApplicationCommand import ApplicationCommand
from argument import *
from pyrfid.asn1.Encoder import Encoder
from pyrfid.asn1.Decoder import Decoder
from pyrfid.Util.Util import Util
from pyrfid.ApplicationInterface.argument.ReadObjectsResponse import ReadObjectsResponse


class ReadObjectsCommand(ApplicationCommand):
    """docstring for ReadObjectCommand"""    
    
    READ_1ST_OBJECTS  = 0
    READ_MULTIPLE_OBJECTS = 1 
    READ_ALL_OBJECTS = 2
    
    def __init__(self, tagId, readType, readObjectsList = []):
        self.COMMAND_MODULE_OID = '28FC597E11'
        self.RESPONSE_MODULE_OID = '28FC597F11'
        self.TAG_ID = tagId
        self.READ_TYPE = readType
        self.dataSets = []
        self.listCmdAndData = []
        self.ENCODED_COMMAND = ''
        self.READ_OBJECTS_LIST = readObjectsList
        
    def encode(self):
        
        encoder = Encoder()
        
        self.listCmdAndData.append(['06', '05', self.COMMAND_MODULE_OID])
        self.listCmdAndData.append(['30', '', ''])
        
        #tagID
        lenTagIDHex = encoder.lengthEncode(len(self.TAG_ID) / 2)
        self.listCmdAndData.append([self.OCTET_STRING, lenTagIDHex, self.TAG_ID])
        
        #Read Type
        self.listCmdAndData.append([self.INTEGER, '01', encoder.intEncode(self.READ_TYPE)])
        
        self.listCmdAndData.append(['30','', ''])
        addObjectLength = 0
        for readObject in self.READ_OBJECTS_LIST:
            
            objectIdentifier = encoder.oidEncode(readObject.OBJECT_IDENTIFIRE)
            lenOIDHex = encoder.lengthEncode(len(objectIdentifier) / 2)
            self.listCmdAndData.append([self.OBJECT_IDENTIFIER, lenOIDHex, objectIdentifier])
            addObjectLength += int(lenOIDHex, 16)
            
            self.listCmdAndData.append([self.BOOLEAN , '01', encoder.boolEncode(readObject.CHECK_DUPLICATE)])
            addObjectLength += 1
        
        self.listCmdAndData[4][1] = str(encoder.lengthEncode(addObjectLength))
        self.listCmdAndData[1][1] = Util.convertIntToHex(int(lenTagIDHex, 16) + 1)
        
        self._convertToHexString()
        return self.ENCODED_COMMAND
    
    
    def decode(self, encodedRSP): # TODO
 
        decoder = Decoder(encodedRSP)
        encoder = Encoder()
        print "----"
        print decoder
        print "----"
        encoder.oidEncode(decoder.popElement().VALUE)
        # sys.exit()
        # if encoder.oidEncode(decoder.popElement().VALUE) != self.RESPONSE_MODULE_OID:
        #     raise "ERROR"
        
        #pop 30
        decoder.popElement()
        
        #completion code:INTEGER
        self.completionCode = decoder.popElement().VALUE
        #execution code:INTEGER
        self.executionCode = decoder.popElement().VALUE
        #objects:SEQUENCE OF SEQUENCE
        decoder.popElement().VALUE
        
        dataSetArray = []
        readObjectsResponseList = []
        for index in range(decoder.getLengthElement() / 4):
            
            #objectId:OBJECT_IDENTIFIER
            objectIdentifier = decoder.popElement().VALUE
            #object:OCTET_STRING
            objectValue = decoder.popElement().VALUE
            #compactParameter:INTEGER
            compactParameter = decoder.popElement().VALUE
            #lockStatus:BOOLAN
            lockStatus = decoder.popElement().VALUE
            readObjectsResponseList.append(ReadObjectsResponse(objectIdentifier, objectValue, compactParameter, lockStatus, 0))
                
        return {'CompletionCode':self.completionCode, 'ExecutionCode':self.executionCode, 'ReadObjectsResponseList':readObjectsResponseList}
    
if __name__ == '__main__':
    
    tagId = "E200C0D2B25FD380"
    command = ReadObjectsCommand(tagId, ReadObjectsCommand.READ_ALL_OBJECTS)
    print command.encode()
    responseCode = "060528FC597F12302A0201000201003028060628FC5909180104053132333435020101010100060728FC5909853C010412323030382F31322F31382030323A34313A25020104010100"
    result =  command.decode(responseCode)
    print result
    
